package pk.nak.responses;

import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.google.android.maps.GeoPoint;

import pk.nak.overlays.EatOutOverlayItem;
import pk.nak.utils.XMLUtils;

public class GetLocationsResponse extends BaseResponse {
	private String message;
	private ArrayList<EatOutOverlayItem> overlays;
	
	@Override
	public void onSuccess(String response) {
		try{
			this.response = response;
			DocumentBuilderFactory dbfLoginResponse = DocumentBuilderFactory.newInstance();
	        DocumentBuilder dbLoginResponse = dbfLoginResponse.newDocumentBuilder();
	        
	        Document dom = dbLoginResponse.parse(new InputSource(new StringReader(response)));
	        NodeList nlmenu = dom.getElementsByTagName("location");
	        
	        this.overlays = new ArrayList<EatOutOverlayItem>();
	        
	        if ( nlmenu != null && nlmenu.getLength() > 0 ){
	        	 for ( int i = 0 ; i < nlmenu.getLength(); i ++){
	        		Element eleDish = (Element)nlmenu.item(i);

	        		String title = "Eatout";
	        		String description = XMLUtils.getTextValue(eleDish, "placename");
	        		String strlat = XMLUtils.getTextValue(eleDish, "latitude");
	        		String strlng = XMLUtils.getTextValue(eleDish, "longitude");
	        		
	        		int lat =(int)(Double.parseDouble(strlat) * 1e6);
	        		int lng = (int)(Double.parseDouble(strlng) * 1e6);
	        		
	        		this.overlays.add(new EatOutOverlayItem(new GeoPoint(lat,lng), title, description));
	        	}
	        }
		}catch(Exception ex){
			
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public ArrayList<EatOutOverlayItem> Overlays(){
		return this.overlays;
	}
}
